package cc.fine.svc.main.service.impl;

import cc.fine.svc.config.redis.utils.RedisCache;
import cc.fine.svc.main.bean.VideoOpCount;
import cc.fine.svc.main.bean.es.VideoEsEntity;
import cc.fine.svc.main.service.VideoCountService;
import cc.fine.svc.main.service.tbl.TblVideoService;
import cc.fine.svc.main.utils.EsUtil;
import com.alibaba.fastjson2.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;

/**
 * @AUTHOR: wyihe
 * @DATE: 2023/10/28
 */
@Service
public class VideoCountServiceImpl implements VideoCountService {

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private EsUtil esUtil;

    @Override
    public VideoOpCount get(String videoId) {
        VideoOpCount videoOpCount = redisCache.getCacheMapValue(VIDEO_COUNT_MAP_CACHE, videoId);
        return videoOpCount;
    }

    @Override
    public void increaseView(String videoId) {
        VideoOpCount videoOpCount = redisCache.getCacheMapValue(VIDEO_COUNT_MAP_CACHE, videoId);
        videoOpCount.setViewCount(videoOpCount.getViewCount() + 1);
        redisCache.setCacheMapValue(VIDEO_COUNT_MAP_CACHE, videoId, videoOpCount);

        updateEsData(videoId, videoOpCount);
    }

    @Override
    public void increaseFavor(String videoId) {
        VideoOpCount videoOpCount = redisCache.getCacheMapValue(VIDEO_COUNT_MAP_CACHE, videoId);
        videoOpCount.setFavorCount(videoOpCount.getFavorCount() + 1);
        redisCache.setCacheMapValue(VIDEO_COUNT_MAP_CACHE, videoId, videoOpCount);

        updateEsData(videoId, videoOpCount);
    }

    @Override
    public void increaseCollect(String videoId) {
        VideoOpCount videoOpCount = redisCache.getCacheMapValue(VIDEO_COUNT_MAP_CACHE, videoId);
        videoOpCount.setCollectCount(videoOpCount.getCollectCount() + 1);
        redisCache.setCacheMapValue(VIDEO_COUNT_MAP_CACHE, videoId, videoOpCount);

        updateEsData(videoId, videoOpCount);
    }

    @Override
    public void increaseShare(String videoId) {
        VideoOpCount videoOpCount = redisCache.getCacheMapValue(VIDEO_COUNT_MAP_CACHE, videoId);
        videoOpCount.setShareCount(videoOpCount.getShareCount() + 1);
        redisCache.setCacheMapValue(VIDEO_COUNT_MAP_CACHE, videoId, videoOpCount);

        updateEsData(videoId, videoOpCount);
    }

    @Override
    public void increaseCommon(String videoId) {
        VideoOpCount videoOpCount = redisCache.getCacheMapValue(VIDEO_COUNT_MAP_CACHE, videoId);
        videoOpCount.setCommonCount(videoOpCount.getCommonCount() + 1);
        redisCache.setCacheMapValue(VIDEO_COUNT_MAP_CACHE, videoId, videoOpCount);

        updateEsData(videoId, videoOpCount);
    }

    @Override
    public void reduceFavor(String videoId) {
        VideoOpCount videoOpCount = redisCache.getCacheMapValue(VIDEO_COUNT_MAP_CACHE, videoId);
        videoOpCount.setFavorCount(videoOpCount.getFavorCount() - 1);
        redisCache.setCacheMapValue(VIDEO_COUNT_MAP_CACHE, videoId, videoOpCount);

        updateEsData(videoId, videoOpCount);

    }

    @Override
    public void reduceCollect(String videoId) {
        VideoOpCount videoOpCount = redisCache.getCacheMapValue(VIDEO_COUNT_MAP_CACHE, videoId);
        videoOpCount.setCollectCount(videoOpCount.getCollectCount() - 1);
        redisCache.setCacheMapValue(VIDEO_COUNT_MAP_CACHE, videoId, videoOpCount);

        updateEsData(videoId, videoOpCount);

    }


    @Override
    public void reduceCommon(String videoId) {
        VideoOpCount videoOpCount = redisCache.getCacheMapValue(VIDEO_COUNT_MAP_CACHE, videoId);
        videoOpCount.setCommonCount(videoOpCount.getCommonCount() - 1);
        redisCache.setCacheMapValue(VIDEO_COUNT_MAP_CACHE, videoId, videoOpCount);

        updateEsData(videoId, videoOpCount);

    }

    /**
     * \
     * 通用的更新
     *
     * @param vid
     */
    private void updateEsData(String vid, VideoOpCount count) {
        VideoEsEntity entity = new VideoEsEntity();
        entity.setCount(count);
        try {
            boolean b = esUtil.existsById(TblVideoService.Es_Index, vid);
            if (!b) {
                esUtil.addData(JSON.toJSONString(entity), TblVideoService.Es_Index, vid);
            }else {
                esUtil.updateDataById(entity, TblVideoService.Es_Index, vid);
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
